/**
 * 
 */
package ivan.cheng.tuqu.adapter;

import ivan.cheng.tuqu.Globals;
import ivan.cheng.tuqu.R;
import ivan.cheng.tuqu.TuQulication;
import ivan.cheng.tuqu.crop.Util;
import ivan.cheng.tuqu.util.BitmapUtil;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * 
 * @author ivan
 *
 */
public class GridTextPopAdapter extends BaseAdapter{

	/**
	 * 
	 */
	private LayoutInflater mInflater;
	private Context mContext;
	public String[] acceLst;
	private List<String> mlst = new ArrayList<String>();
	private List<WeakReference<Bitmap>> mBitmapLst;
	private float fWidth;
	private TuQulication mApp;
	public GridTextPopAdapter(Context _context,TuQulication app,List<WeakReference<Bitmap>> bitmapLst) {
		mInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContext = _context;
		try {
			acceLst = mContext.getAssets().list("textpop");
		} catch (IOException e) {
			e.printStackTrace();
		}
		fWidth = Float.valueOf(app.getScreenWidth());
		mBitmapLst = bitmapLst;
		mApp = app;
		//mlst = ThemeSpecDownLoadManager.getInstance(app).getSpecEffectPathList(2);
	}

	public int getCount() {
//		if(mlst.size() != ThemeSpecDownLoadManager.getInstance(mApp).getSpecEffectPathList(2).size())
//		{
//			mlst.clear();
//			mlst = ThemeSpecDownLoadManager.getInstance(mApp).getSpecEffectPathList(2);
//		}
		return acceLst.length + 1/* + mlst.size()*/;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ImageView i = null;

        if (convertView == null) {
        	if(0 == position)
        	{
        		convertView = mInflater.inflate(R.layout.item_download_more, null);
        		convertView.setLayoutParams(new Gallery.LayoutParams(parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
    					5, mContext.getResources().getDisplayMetrics()), 
    					parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
    	    					5, mContext.getResources().getDisplayMetrics())));
        		convertView.setTag(R.id.image_download_more,R.id.image_download_more);
        		return convertView;
        	}
        	else
        	{
        		 i = new ImageView(mContext);
                 i.setScaleType(ImageView.ScaleType.FIT_XY);
        	}
            i.setLayoutParams(new Gallery.LayoutParams(parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
					5, mContext.getResources().getDisplayMetrics()), 
					parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
	    					5, mContext.getResources().getDisplayMetrics())));
        } else {
            i = (ImageView) convertView;
        }
        Bitmap bmp = null;
        if(mBitmapLst.size() > position - 1 
        		&& null != mBitmapLst.get(position - 1))
        {
        	if(null != mBitmapLst.get(position - 1).get()
            	&&!mBitmapLst.get(position - 1).get().isRecycled())
        	{
        		bmp = mBitmapLst.get(position - 1).get();
        	}
        	else
        	{
        		try {
        			if(position < mlst.size() + 1)
        				bmp = BitmapUtil.instance().DecodeFromFile(mlst.get(position - 1).replace(Globals.MX_FILE_PATH_SPEC_EFFECT_FOAM,
        						Globals.MX_FILE_PATH_SPEC_EFFECT_IMG));
        			else
        				bmp = BitmapUtil.instance().DecodeFromStream(mContext.getAssets().open("textpop_thumb/" + acceLst[position - 1 - mlst.size()]));
        				
        		} catch (IOException e) 
        		{
        			e.printStackTrace();
        		}
        		mBitmapLst.set(position - 1, new WeakReference<Bitmap>(bmp));
        	}
        }
        else
        {
        	try {
    			if(position < mlst.size() + 1)
    				bmp = BitmapUtil.instance().DecodeFromFile(mlst.get(position - 1).replace(Globals.MX_FILE_PATH_SPEC_EFFECT_FOAM,
    						Globals.MX_FILE_PATH_SPEC_EFFECT_IMG));
    			else
    				bmp = BitmapUtil.instance().DecodeFromStream(mContext.getAssets().open("textpop_thumb/" + acceLst[position - 1 - mlst.size()]));
    		} catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    		mBitmapLst.add(new WeakReference<Bitmap>(bmp));
        }
		
		if(position < mlst.size() + 1)
			i.setTag(R.id.image_file_path, mlst.get(position - 1));
		else
			i.setTag(R.id.image_assert_path, "textpop/" + acceLst[position - 1 - mlst.size()]);
		i.setTag(R.id.image_matrix_scale, fWidth/Util.GetBigImageDimesion(mContext));
		i.setPadding(2, 2, 2, 2);
		i.setBackgroundResource(R.drawable.beauty_element_frame);
		i.setImageBitmap(bmp);
        return i;
	}

}
